<?php
include "./address.php";

// 获取省
$province_arr = [];
foreach($address as $k=>$v){
	$province_arr[] = $v['province_name'];
}

// echo "<pre>";
// print_r($province_arr);
// echo "</pre>";

// 接收省；
$proName = $_GET['pro'];
// echo $proName;
$citys = [];
foreach($address as $v){
	if($v['province_name']==$proName){
		foreach($v['city'] as $vv){
			$citys[] = $vv['city_name'];
		}
	}
}

// print_r($citys);


$chioseProName = $_GET['pro'];
// echo $chioseProName;




?>



<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		省 : <select class="pro" value="安徽省">
			<?php foreach($province_arr as $v){ ?>
				<option <?php echo $chioseProName==$v?'selected':'';  ?> ><?php echo $v;?></option>
			<?php } ?>
		</select>	
		
		市：  <select class="citys">
			<?php foreach($citys as $v){ ?>
				<option><?php echo $v;?></option>
			<?php } ?>
		</select>	
		<script>
			
			// 获取get里的pro参数 判断 option 是否需要selected选中；
			
			var proEle = document.querySelector(".pro");
			// var pro = window.location.search;
			// var proName = pro.substr(1);
			// // console.log(proName);
			// var arr = proName.split("=");
			// var prostr = decodeURI(arr[1]);
			// var opts = document.querySelectorAll(".pro option");
			// // console.log(opts);
			// opts.forEach(item=>{
			// 	// console.log(item.value);
			// 	if(item.value==prostr){
			// 		item.setAttribute("selected","selected");
			// 	}
			// })
			// console.log(prostr);
			// console.log(decodeURI(prostr));
			
			proEle.onchange = function(){
				// console.log(123);
				var val = this.value;
				// console.log(val);
				window.location.href="./index.php?pro="+encodeURI(val);
			}
		</script>
	</body>
</html>
